package com.starsea.book.ui.newpage.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.tools.r8.internal.bo
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityBookDepositoryChildBinding
import com.starsea.book.databinding.ActivityBookRanksBinding
import com.starsea.book.ui.book.info.BookInfoActivity
import com.starsea.book.ui.newpage.adapter.BookDepositoryScreenOneAdapter
import com.starsea.book.ui.newpage.adapter.BookDepositoryScreenOtherAdapter
import com.starsea.book.ui.newpage.adapter.SearchBookResultAdapter
import com.starsea.book.ui.newpage.entity.BookTabTitleEntity
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @desc : 书库
 */
class BookDepositoryChildActivity :
    VMBaseActivity<ActivityBookDepositoryChildBinding, HomeViewModel>() {

    lateinit var mAdapterOne: BookDepositoryScreenOneAdapter
    lateinit var mAdapterTwo: BookDepositoryScreenOtherAdapter
    lateinit var mAdapterThree: BookDepositoryScreenOtherAdapter
    lateinit var mAdapterFour: BookDepositoryScreenOtherAdapter

    lateinit var mAdapter: SearchBookResultAdapter


    var bookState: String = "-1"
    var labelTitle: String = ""
    var pageNo: Int = 1
    var sortType: String = "-1"
    var typeId: String = ""
    var wordCount: String = "-1"

    override val binding by viewBinding(ActivityBookDepositoryChildBinding::inflate)
    override val viewModel by viewModels<HomeViewModel>()

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        binding.titleBarView.setOnLeftTextClickListener {
            finish()
        }

        mAdapterOne = BookDepositoryScreenOneAdapter(this)
        mAdapterTwo = BookDepositoryScreenOtherAdapter(this)
        mAdapterThree = BookDepositoryScreenOtherAdapter(this)
        mAdapterFour = BookDepositoryScreenOtherAdapter(this)

        mAdapterOne.addOnItemChildClickListener(R.id.tv_title) { _, _, pos ->
            mAdapterOne.items.forEach { it.isSelect = false }
            bookState = mAdapterOne.items[pos].value
            mAdapterOne.items[pos].isSelect = true
            mAdapterOne.notifyDataSetChanged()
            pageNo = 1
            viewModel.getBookDepositoryList(
                bookState,
                labelTitle,
                pageNo,
                sortType,
                typeId,
                wordCount
            )
        }
        mAdapterTwo.addOnItemChildClickListener(R.id.tv_title) { _, _, pos ->
            mAdapterTwo.items.forEach { it.isSelect = false }
            labelTitle = mAdapterTwo.items[pos].value
            mAdapterTwo.items[pos].isSelect = true
            mAdapterTwo.notifyDataSetChanged()
            pageNo = 1
            viewModel.getBookDepositoryList(
                bookState,
                labelTitle,
                pageNo,
                sortType,
                typeId,
                wordCount
            )
        }
        mAdapterThree.addOnItemChildClickListener(R.id.tv_title) { _, _, pos ->
            mAdapterThree.items.forEach { it.isSelect = false }
            wordCount = mAdapterThree.items[pos].value
            mAdapterThree.items[pos].isSelect = true
            mAdapterThree.notifyDataSetChanged()
            pageNo = 1
            viewModel.getBookDepositoryList(
                bookState,
                labelTitle,
                pageNo,
                sortType,
                typeId,
                wordCount
            )
        }
        mAdapterFour.addOnItemChildClickListener(R.id.tv_title) { _, _, pos ->
            mAdapterFour.items.forEach { it.isSelect = false }
            sortType = mAdapterFour.items[pos].value
            mAdapterFour.items[pos].isSelect = true
            mAdapterFour.notifyDataSetChanged()
            pageNo = 1
            viewModel.getBookDepositoryList(
                bookState,
                labelTitle,
                pageNo,
                sortType,
                typeId,
                wordCount
            )
        }


        binding.rvOne.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        binding.rvOne.adapter = mAdapterOne

        binding.rvTwo.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        binding.rvTwo.adapter = mAdapterTwo

        binding.rvThree.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        binding.rvThree.adapter = mAdapterThree

        binding.rvFour.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        binding.rvFour.adapter = mAdapterFour


        mAdapter = SearchBookResultAdapter(this)
        binding.rvContent.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        binding.rvContent.adapter = mAdapter


        typeId = intent.getStringExtra("typeId") ?: ""

        viewModel.getBookListFilter(typeId)

        viewModel.bookListFilterResutl.observe(this) {
            if (it.status == 200) {
                mAdapterOne.submitList(it.data!!.bookStateList)
                mAdapterTwo.submitList(it.data!!.labelTitleList)
                mAdapterThree.submitList(it.data!!.wordCountList)
                mAdapterFour.submitList(it.data!!.sortTypeList)
            } else {
                toastOnUi(it.msg)
            }
        }


        viewModel.getBookDepositoryList(bookState, labelTitle, pageNo, sortType, typeId, wordCount)


        binding.srlRefresh.setOnRefreshListener {
            pageNo = 1
            viewModel.getBookDepositoryList(
                bookState,
                labelTitle,
                pageNo,
                sortType,
                typeId,
                wordCount
            )
        }
        binding.srlRefresh.setOnLoadMoreListener {
            pageNo++
            viewModel.getBookDepositoryList(
                bookState,
                labelTitle,
                pageNo,
                sortType,
                typeId,
                wordCount
            )
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity(
                BookInfoActivity::class,
                "bookId" to mAdapter.items[position].bookId!!
            )
        }

        viewModel.bookDepositoryListResutl.observe(this) {
            if (it.status == 200) {
                if (pageNo == 1) {
                    mAdapter.submitList(it.data)
                    binding.srlRefresh.finishRefresh()
                } else {
                    binding.srlRefresh.finishLoadMore()
                    mAdapter.addAll(it.data!!)

                }
            } else {
                toastOnUi(it.msg)
            }
        }


    }


}